import router from "@/router/router";
import { GET_TOKEN } from "@/utils/token";
import api from "@/api/api";
const user = {
  state: {
    //token
    token: GET_TOKEN(),
    //用户信息
    userInfo: {},
    // 菜单
    menuRoutes: router.options.routes,
  },
  mutations: {
    //设置token
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    //设置用户信息
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
  },
  getters: {
    token: (state) => state.token,
    //是否登录
    isLogin: (state) => Boolean(state.userInfo.user),
    //用户信息
    getUserInfo: (state) => state.userInfo || {},
  },
  actions: {
    async userInfo(context) {
      try {
        let result = await api.getUserInfo(this.token)
        const checkUser = result.data.checkUser
        if(result.code == 200){
          context.commit("SET_USERINFO",checkUser)
        }
      } catch (error) {
        console.log("error:",error);
      }
    },
  },
};
export default user;
